recent_items = false;
cookie_data  = false;
toolbar_bottom_state = false;
toolbar_bottom_keyboard = false;
active_notify = false;
var isIE6 = jQuery.browser.msie && parseFloat(jQuery.browser.version) < 7;

jQuery(document).ready(function(){
	
	ajaxGetNotification();
	
	/*if(isIE6)
	{
		jQuery.ifixpng(WEB_DIR + 'javascript/toolbar/images/toolbar_mini.png');
	}*/
	
	if(jQuery.cookie('toolbar_bottom_state')) toolbar_bottom_state = jQuery.cookie('toolbar_bottom_state');
	
	if((toolbar_bottom_state)&&(toolbar_bottom_state == 'full'))
	{
		jQuery("#toolbar-wrapper").slideUp("fast");
		jQuery("#toolbarbut").fadeIn("slow");
		if(parseInt(jQuery('#num_message_mini').html()) > 0)
		{
			jQuery("#alert-msg_tb_mini").fadeIn();
		}
	}
	else
	{
		//alert(jQuery('#num_message_mini').html());
		jQuery("#toolbar-wrapper").slideDown("fast");
    	jQuery("#toolbarbut").fadeOut();
		
		if(parseInt(jQuery('#num_message_tb').html()) > 0)
		{
			jQuery("#alert-msg_tb").fadeIn();
		}
	}
	
	if(jQuery.cookie('toolbar_bottom_keyboard')) toolbar_bottom_keyboard = jQuery.cookie('toolbar_bottom_keyboard');
	
	if(toolbar_bottom_keyboard)
	{
		nag_toolbar_bottom_keyboard = toolbar_bottom_keyboard == 'searchLeftOn'? 'searchLeftOff': 'searchLeftOn';
		jQuery('#keyStatus').addClass(toolbar_bottom_keyboard).removeClass(nag_toolbar_bottom_keyboard);
		jQuery('#keyStatus_full').addClass(toolbar_bottom_keyboard).removeClass(nag_toolbar_bottom_keyboard);
		if(toolbar_bottom_keyboard == 'searchLeftOn')
		{
			Mudim.SetMethod(2);
		}
		else
		{
			Mudim.SetMethod(0);
		}
	}
	
  	//hide toolbar and make visible the 'show' button
	jQuery("div#toolbar_end").click(function() {
		if(jQuery('#box-slide').attr('lang') == 'show')
		{
			jQuery('#box-slide').attr('lang','hide').hide();
			jQuery.unblockUI();
		}
		if(jQuery('#toolbar_bottom_notify').attr('lang') == 'show')
		{
			jQuery('#toolbar_bottom_notify').fadeOut('fast').attr('lang','hide');
		}
		jQuery("#toolbar-wrapper").slideUp("fast");
		jQuery("#toolbarbut").fadeIn("slow");
		if(parseInt(jQuery('#num_message_tb').html()) > 0)
		{
			jQuery("#alert-msg_tb").fadeIn();
		}
		jQuery.cookie('toolbar_bottom_state', 'full', {path: '/', expires: 16});
	});
  
  	//show toolbar and hide the 'show' button
  	jQuery("div#toolbarbut_switch").click(function() {
		if(jQuery('#toolbar_bottom_notify').attr('lang') == 'show')
		{
			jQuery('#toolbar_bottom_notify').fadeOut('fast').attr('lang','hide');
		}
    	jQuery("#toolbar-wrapper").slideDown("fast");
    	jQuery("#toolbarbut").fadeOut();    
		if(parseInt(jQuery('#num_message_mini').html()) > 0)
		{
			jQuery("#alert-msg_tb_mini").fadeIn();
		}
		jQuery.cookie('toolbar_bottom_state', 'mini', {path: '/', expires: 16});
  	});
  
  	jQuery('#toolbar_end').mouseover(function(){
		jQuery('#toolbar_end').addClass('tb_end_hover').removeClass('tb_end');
	});
  	jQuery('#toolbar_end').mouseout(function(){
		jQuery('#toolbar_end').addClass('tb_end').removeClass('tb_end_hover');
	});
	jQuery('#divMiniSubmit').click(function(){
		jQuery('#miniSearch').submit();
	});
	
	jQuery('#MiniHistory').click(function(){
										  
		if(jQuery('#box-slide').attr('lang') == 'hide')
		{
			if(!cookie_data)
			{
				
				recent_items = jQuery.cookie('recent_items');
				if (recent_items)
				{
					jQuery.post(WEB_DIR+"ajax.php?act=item&code=item_history",{
							itemids	: recent_items
						},
						function(json_data){
							cookie_data = json_data;
							show_item_history_mini(1,1);
						},"json"
					); 
				}
				else
				{
					jQuery.blockUI({message: 'Bạn chưa xem sản phẩm nào', css: {bottom:'27px',left:'154px',top:''}, overlayCSS: { backgroundColor: '#000', opacity: 0.5} });
					jQuery('#box-slide').attr('lang','show');
				}
			}
			else
			{
				show_item_history_mini(1,0);
			}
		}
		else
		{
			jQuery('#box-slide').attr('lang','hide').slideUp('fast');
			jQuery.unblockUI();
		}
		
		jQuery('.blockOverlay').attr('title','Click to unblock').click(function(){
			jQuery('#box-slide').attr('lang','hide').slideUp('fast');
			jQuery.unblockUI();
		});
	});
	
});

function ajaxGetNotification()
{
	jQuery().ajaxSend(function(r,s){
		jQuery("#loading-layer").hide();
	});
	jQuery.ajax({
				type: 'GET',
				url:WEB_DIR+"ajax.php?act=personal&code=show_all_notify",
				dataType: 'json',
				success:function(json){
					active_notify = json.active_notify;
					jQuery('#toolbar_bottom_notify_content').html(json.content);
					if(json.total_unread > 0)
					{
						jQuery('#num_message_mini').html(json.total_unread);
						jQuery('#num_message_tb').html(json.total_unread);
						jQuery("#alert-msg_tb").fadeIn();
						jQuery("#alert-msg_tb_mini").fadeIn();
					}
					setTimeout(function(){ajaxGetNotification()},30000);
				}
			}); 
	jQuery().ajaxSend(function(r,s){
		jQuery("#loading-layer").show();
	});
	return false;
}

function openBTNotify()
{
	if(jQuery('#toolbar_tooltip')&&(jQuery('#toolbar_tooltip').css('display') == 'block'))
	{
		jQuery('#toolbar_tooltip').hide();
	}
	
	if(jQuery('#toolbar_bottom_notify').attr('lang') == 'hide')
	{
		jQuery('#toolbar_bottom_notify').fadeIn('fast').show().attr('lang','show');
		
		if(active_notify&&(jQuery('#num_message_tb').html() > 0))
		{
			item_ids_ar = '';
			for(i in active_notify.item_ids_ar)
			{
				if(!isNaN(i))
				{
					item_ids_ar += (item_ids_ar?',':'') + active_notify.item_ids_ar[i];
				}
			}

			item_ids_ar2 = '';
			for(i in active_notify.item_ids_ar2)
			{
				if(!isNaN(i))
				{
					item_ids_ar2 += (item_ids_ar2?',':'') + active_notify.item_ids_ar2[i];
				}
			}

			c_user_ids_ar = '';
			for(i in active_notify.c_user_ids_ar)
			{
				if(!isNaN(i))
				{
					c_user_ids_ar += (c_user_ids_ar?',':'') + active_notify.c_user_ids_ar[i];
				}
			}

			pm_ids_ar = '';
			for(i in active_notify.pm_ids_ar)
			{
				if(!isNaN(i))
				{
					pm_ids_ar += (pm_ids_ar?',':'') + active_notify.pm_ids_ar[i];
				}
			}
			
			jQuery.ajax({
					type: 'GET',
					url:WEB_DIR+"ajax.php?act=personal&code=update_notify_read",
					data: {
						item_ids_ar		: item_ids_ar,
						item_ids_ar2	: item_ids_ar2,
						c_user_ids_ar	: c_user_ids_ar,
						pm_ids_ar		: pm_ids_ar
					},
					success:function(retVal){
						if(retVal == 'success')
						{
							setTimeout(function(){
												jQuery('.BTNotify_unread').css('background','#ffffff');
												jQuery('#num_message_mini').html('0');
												jQuery('#num_message_tb').html('0');
												jQuery("#alert-msg_tb").fadeOut();
												jQuery("#alert-msg_tb_mini").fadeOut();
												},5000);
							
						}
					}
				}); 
		}
		jQuery('#toolbar_bottom_notify_close').click(function(){
			jQuery('#toolbar_bottom_notify').fadeOut('fast').attr('lang','hide');
		});
	}
	else
	{
		jQuery('#toolbar_bottom_notify').fadeOut('fast').attr('lang','hide');
	}
}

function showBTTooltip(id)
{
	if(jQuery('#' + id)&&(jQuery('#' + id).css('display') == 'none'))
	{
		jQuery('#' + id).show();
	}
}

function hideBTToolTip(id)
{
	if(jQuery('#' + id)&&(jQuery('#' + id).css('display') == 'block'))
	{
		jQuery('#' + id).hide();
	}
}


function show_his_page_mini(page_show, page_hide){
	if(page_hide && page_show!=page_hide){
		jQuery("#his_page_mini_" + page_show).show();
		jQuery("#his_page_mini_" + page_hide).hide();		
	}
}

function show_item_history_mini(show_page,rebuild){
	if(typeof(show_page) == 'undefined' || show_page == 1)
	show_page = 0;
	
	if(typeof(rebuild) == 'undefined')
	rebuild = 0;
	
	var re_items = '';
	if(typeof(rebuild) == 'undefined') rebuild = 0;
	var html_content = '';
	
	if(cookie_data.length){
		

		html_content = '<div class="slide-mid"><div class="slide-title">Các tin bạn đã xem</div>';
		
		var page = 1;
		var gstt = 1;
		var max_page = Math.ceil(cookie_data.length/6);
		var new_page = 1;
		
		for (var i = 0; i < cookie_data.length; i++){
			if(rebuild){
				re_items += (re_items!=''?',':'') + '' + cookie_data[i].item_id + '';
			}
			
			if(new_page){
				new_page = 0;
				
				var page_pre = page - 1;
				if(i == 0){
					page_pre = max_page;
				}
				
				html_content +=  '<div id = "his_page_mini_' + page +'" ' + (page>1?'style="display:none"':'') + '><div style="float:right;margin-top:7px;margin-right:10px;"><a id="mini_history_item_close" style="cursor:pointer;">[Đóng]</a></div><div class="slide-paging" style="padding-right:15px;">Trang <b>' + page +'</b>/<b>' + max_page + '</b></div><div class="slide-content"><div class="box-btn-slide-left"><input type="button" onmouseout="this.className=\'btnSlideLeft\'" onmouseover="this.className=\'btnSlideLeftOver\'" name="btnSlideLeft" class="btnSlideLeft" onclick="show_his_page_mini(' + page_pre + ',' + page + ');"/></div><div class="box-slide-content">';
			}
			var price = "";
			
			if(cookie_data[i].price != "undefined" && cookie_data[i].price){
				price = cookie_data[i].price + ' <img src="' + WEB_DIR + 'style/images201005/ico-vnd.gif" alt="VNĐ"/>';
			}
			
			html_content += '<div class="slide-img" lang="'+ cookie_data[i].item_id +'"><img class="element-del" src="' + WEB_DIR + 'style/images/list_del_hover.gif" width="13" height="13" title="Loại khỏi danh sách Tin đã xem" id="ck_del_mini_'+ cookie_data[i].item_id +'" onclick="ck_remove_mini('+ cookie_data[i].item_id  + ',' + page + ');"/>\
							<div class="box-slide-img" '+ (cookie_data[i].item_image != ''? '' : 'style="background:url(../../style/images/no-images-69x53.jpg) no-repeat center') +'">\
								<a href="'+cookie_data[i].item_url+'"><img src="'+ (cookie_data[i].item_image != ''? cookie_data[i].item_image : 'style/images201005/space.gif' ) + '" class="lazyload" width="120" height="105"/></a>\
							</div>\
							<div class="box-slide-other">\
								<div class="box-slide-other-title">\
									<a href="'+cookie_data[i].item_url+'" title="'+cookie_data[i].item_title+'">'+cookie_data[i].item_title+'</a>\
								</div>\
								<div class="box-slide-other-price">\
									'+price+'\
								</div>\
							</div>\
						</div>';
				
		
			if(gstt == 6 || i == cookie_data.length-1){
				var page_next = page + 1;
				
				if(i == cookie_data.length-1){
					page_next = 1;
				}
				
				html_content += '</div>\
							<div class="box-btn-slide-right">\
								<input type="button" onmouseout="this.className=\'btnSlideRight\'" onmouseover="this.className=\'btnSlideRightOver\'" name="btnSlideRight" class="btnSlideRight" onclick="show_his_page_mini(' + page_next + ',' + page + ');"/>\
							</div>\
						</div>\
					</div>';
					
				gstt = 1;
				page++;
				new_page = 1;
			}
			else{
				gstt++;	
			}
		}

		html_content += '</div>';
		//alert (html_content);
		
	}
	
	if(html_content)
	{
		var leftPos = (jQuery(window).width() - 1000) /2 + 'px';
		jQuery('#box-slide').html(html_content).slideDown("fast").attr('lang','show').css({left: leftPos});
		jQuery.blockUI({message: '', overlayCSS: { backgroundColor: '#000', opacity: 0.5} });
	}
	else
	{
		jQuery.blockUI({message: 'Bạn chưa xem sản phẩm nào', overlayCSS: { backgroundColor: '#000', opacity: 0.5} });
	}
	
	jQuery('.blockOverlay').attr('title','Click to unblock').click(function(){
		jQuery('#box-slide').attr('lang','hide').slideUp('fast');
		jQuery.unblockUI();
	});
	
	jQuery(".slide-img").each(function() {
		jQuery(this).mouseover(function(){
			jQuery("#ck_del_mini_" + this.lang).show();
										 });
		jQuery(this).mouseout(function(){
			jQuery("#ck_del_mini_" + this.lang).hide();
										 });
	});
	
	jQuery('#mini_history_item_close').click(function(){
		jQuery('#box-slide').attr('lang','hide').slideUp('fast');
		jQuery.unblockUI();
	});
	
	
	if(show_page){
		jQuery("#his_page_" + 1).hide();
		jQuery("#his_page_" + show_page).show();
	}
	
	if(rebuild && re_items != recent_items){
		jQuery.cookie('recent_items', re_items, {path: '/', expires: 16});
	}
}

function changeKeyboard(id){
	if (jQuery('#' + id).hasClass('searchLeftOff')){
		//setMethod(1);
		Mudim.SetMethod(2);
		jQuery('#keyStatus').addClass('searchLeftOn').removeClass('searchLeftOff');
		jQuery('#keyStatus_full').addClass('searchLeftOn').removeClass('searchLeftOff');
		jQuery.cookie('toolbar_bottom_keyboard', 'searchLeftOn', {path: '/', expires: 16});
	} 
	else{
		Mudim.SetMethod(0);
		jQuery('#keyStatus').addClass('searchLeftOff').removeClass('searchLeftOn');
		jQuery('#keyStatus_full').addClass('searchLeftOff').removeClass('searchLeftOn');
		jQuery.cookie('toolbar_bottom_keyboard', 'searchLeftOff', {path: '/', expires: 16});
	}
	return true;
	
}

function ck_remove_mini(ck_id,page){
	 var cookie_ids = recent_items.split(",");
	 var stt 	= 0;
	 var tmp_ids = '';
	 
	 for (var i = 0; i <cookie_ids.length; i++){
	   if (!isNaN(cookie_ids[i]) && ck_id != cookie_ids[i] && stt < 35){
			var reid = parseInt(cookie_ids[i]);
			
			if(!isNaN(reid) &&  reid>0 && reid!=''){
				 tmp_ids += (tmp_ids!='' ? ',' : '') + parseInt(cookie_ids[i]);
				 stt++;
			}
		 }
	 }
	 
	if(stt && tmp_ids){
		recent_items = tmp_ids;
	 
		var tem_arr = [];
		stt = 0;
			 
		for(i=0; i<cookie_data.length; i++){
			if(cookie_data[i].item_id != ck_id){
				tem_arr[stt] = {"item_id":""+cookie_data[i].item_id+"","item_url":""+cookie_data[i].item_url+"","item_title":""+cookie_data[i].item_title+"","price":""+cookie_data[i].price+"","item_image":""+cookie_data[i].item_image+""};	
				stt++;
			}
		}
		
		if(stt){
			cookie_data = tem_arr;
			show_item_history_mini(page);
		}
		else{
			recent_items = '';
		}
	}
	else{
		recent_items = '';
	}
	
	if (recent_items == ''){
		document.getElementById("item_history").style.display = 'none';	
	}
	
	// set cookie theo ngay
	jQuery.cookie('recent_items', recent_items, {path: '/', expires: 16});
}